package com.fxsh168.server.activity;

import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;

import com.fxsh168.server.R;
import com.fxsh168.server.base.BaseActivity;
import com.fxsh168.server.interfaces.Constant;
import com.fxsh168.server.entitys.RegiYZMCG;
import com.google.gson.Gson;
import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.callback.StringCallback;

import okhttp3.Call;

public class BindWeixin extends BaseActivity {

    private EditText mPutWeixin;
    private String mGetWeixin, token, mPhone;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_bind_weixin);
        mPutWeixin = (EditText) findViewById(R.id.putWeixin);

    }


    //绑定微信
    public void lijiBindWeixin(View view) {
        //获取输入框微信账号
        mGetWeixin = mPutWeixin.getText().toString().trim();
        //参数：phone 手机号
        //token
        //playwx  支付宝账号
        SharedPreferences sp = getSharedPreferences(Constant.LOGINCLASS, MODE_PRIVATE);
        mPhone = sp.getString("mPhone", "");
        token = sp.getString("token", "");
        if (mGetWeixin != null && !TextUtils.isEmpty(mGetWeixin)) {
            OkHttpUtils.post().addParams("phone", mPhone).addParams("token", token)
                    .addParams("playwx", mGetWeixin).url(Constant.WEIXINBANGDING)
                    .build().execute(new StringCallback() {
                @Override
                public void onError(Call call, Exception e, int id) {
                    showToast("连接不上服务器!");
                }

                @Override
                public void onResponse(String response, int id) {
                    if (response != null) {
                        Log.e("BindWeixin", "==BindWeixin=" + response);
                        RegiYZMCG regiYZMCG = new Gson().fromJson(response, RegiYZMCG.class);
                        String code = regiYZMCG.getCode().toString().trim();
                        if (code.equals(200 + "")) {
                            Toast.makeText(BindWeixin.this, "绑定成功!", Toast.LENGTH_SHORT).show();
                            Intent intent = new Intent(BindWeixin.this, BindAcountActivity.class);
                            intent.putExtra("isBindWeixin", true);
                            BindWeixin.this.setResult(Constant.YIJINGBINDWEIXIN, intent);
                            BindWeixin.this.finish();
                        } else if (code.equals(202 + "")) {
                            Toast.makeText(BindWeixin.this, "此账号已经被绑定!", Toast.LENGTH_SHORT).show();
                        }
                    }
                }
            });
        } else {
            mPutWeixin.setError("微信账户不能为空!");
        }


    }
}
